package com.bsb.acc.inner.impl;

import com.bsb.acc.dao.AccShortcutFieldConfigDao;
import com.bsb.acc.domain.AccShortcutFieldConfig;
import com.bsb.acc.domain.AccShortcutFieldConfigCriteria;
import com.bsb.acc.dto.AccShortcutFieldConfigAllDto;
import com.bsb.acc.dto.AccShortcutFieldConfigDto;
import com.bsb.acc.errorcode.BusinessErrorCode;
import com.bsb.acc.service.AccShortcutFieldConfigService;
import com.smy.framework.core.exception.ServiceException;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: ligc
 * on 2019/2/14
 * @Description
 */
@Service("accShortcutFieldConfigService")
public class AccShortcutFieldConfigServiceImpl implements AccShortcutFieldConfigService {

    @Resource
    private AccShortcutFieldConfigDao accShortcutFieldConfigDao;

    @Override
    @Transactional
    public int publishAccShortcutFieldConfig(AccShortcutFieldConfigAllDto accShortcutFieldConfigAllDto) {
        List<AccShortcutFieldConfig> list = accShortcutFieldConfigAllDto.getList();
        if (null == list || list.isEmpty()) {
            throw new ServiceException(BusinessErrorCode.FAILURE.getCode(), "传入发布快捷栏信息为空");
        }
        AccShortcutFieldConfigCriteria accShortcutFieldConfigCriteria = new AccShortcutFieldConfigCriteria();
        AccShortcutFieldConfigCriteria.Criteria criteria = accShortcutFieldConfigCriteria.createCriteria();
        criteria.andStatusEqualTo(1);
        criteria.andDeleteEqualTo(0);
        List<AccShortcutFieldConfig> publishedList = accShortcutFieldConfigDao
                .selectByCriteria(accShortcutFieldConfigCriteria);
        Date date = new Date();
        String user = list.get(0).getCreateUser();
        for (AccShortcutFieldConfig entity : publishedList) {
            entity.setUpdatedTime(date);
            entity.setStatus(2);
            entity.setCreateUser(user);
        }
        accShortcutFieldConfigDao.batchStop(publishedList);

        for (AccShortcutFieldConfig entity : list) {
            entity.setUpdatedTime(date);
            entity.setStatus(1);
            entity.setCreateUser(user);
        }
        accShortcutFieldConfigDao.batchPublish(list);
        return 0;
    }

    @Override
    public AccShortcutFieldConfig save(String dir, String user, String id) {
        Date date = new Date();
        Long key = Long.parseLong(id);
        AccShortcutFieldConfig accShortcutFieldConfig = accShortcutFieldConfigDao.selectByPrimaryKey(key);
        accShortcutFieldConfig.setCreateUser(user);
        accShortcutFieldConfig.setUpdatedTime(date);
        accShortcutFieldConfig.setDelete(1);
        accShortcutFieldConfigDao.updateByPrimaryKeySelective(accShortcutFieldConfig);
        AccShortcutFieldConfig saveEntity = new AccShortcutFieldConfig();
        saveEntity.setImageUrl(dir);
        saveEntity.setCreateUser(user);
        saveEntity.setCreatedTime(date);
        saveEntity.setUpdatedTime(date);
        saveEntity.setStatus(2);
        return saveEntity;
    }

    @Override
    public List<AccShortcutFieldConfig> getAccShortcutFieldConfigByType(Integer status) {
        AccShortcutFieldConfigCriteria accShortcutFieldConfigCriteria = new AccShortcutFieldConfigCriteria();
        AccShortcutFieldConfigCriteria.Criteria criteria = accShortcutFieldConfigCriteria.createCriteria();
        criteria.andDeleteEqualTo(0);
        criteria.andStatusEqualTo(status);
        List<AccShortcutFieldConfig> list = accShortcutFieldConfigDao
                .selectByCriteria(accShortcutFieldConfigCriteria);
        return list;
    }

    @Override
    public AccShortcutFieldConfig updateByFileds(AccShortcutFieldConfigDto accShortcutFieldConfigDto) {
        Integer updatetype = accShortcutFieldConfigDto.getUpdatetype();
        AccShortcutFieldConfig accShortcutFieldConfig = accShortcutFieldConfigDao
                .selectByPrimaryKey(accShortcutFieldConfigDto.getId());
        if ("1".equals(updatetype)) {
            String url = accShortcutFieldConfigDto.getUrl();
            if (StringUtils.isEmpty(url)) {
                url = null;
            }
            accShortcutFieldConfig.setUrl(null);

        } else if ("2".equals(updatetype)) {
            String remark = accShortcutFieldConfigDto.getRemark();
            if (StringUtils.isEmpty(remark)) {
                throw new ServiceException(BusinessErrorCode.FAILURE.getCode(), "修改快捷栏配置备注不能为空！");
            }
            accShortcutFieldConfig.setRemark(remark);
        } else if ("3".equals(updatetype)) {
            String name = accShortcutFieldConfigDto.getName();
            if (StringUtils.isEmpty(name)) {
                throw new ServiceException(BusinessErrorCode.FAILURE.getCode(), "修改快捷栏配置名称不能为空！");
            }
            accShortcutFieldConfig.setName(name);
        }
        accShortcutFieldConfigDao.updateByPrimaryKeySelective(accShortcutFieldConfig);
        return accShortcutFieldConfig;
    }

}
